package cn.u313.entry;

import cn.u313.comm.eunm.BlessingConst;
import cn.u313.comm.utils.ResultBlessing;
import cn.u313.comm.utils.Util;
import cn.u313.config.NewYearConfig;
import cn.u313.listener.BaseListen;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

import java.util.ArrayList;
import java.util.List;

import static cn.u313.comm.utils.Util.icon;
import static cn.u313.config.NewYearConfig.*;

@Getter
@Setter
@Entity
@Table(name = "t_blessing_2023")
public class BlessingEntry {
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Id
    private int id;
    private int jianKang;
    private int xingFu;
    private int jingSheng;
    private int zhaoCai;
    private int pingAn;
    private int haoYun;

//    private int yunQi;//运气牛
//    private int renQI;//人气牛
//    private int xiQi;//喜气牛
//    private int heQi;//和气牛
//    private int caiQi;//财气牛
    private int flag;//是否集齐 0/1
    private int universal;//万能


    public String getHb() {
        return hb;
    }

    public void setHb(String hb) {
        this.hb = hb;
    }

    private String qq;//QQ
    private String time;//开始时间
    private String time0;//集齐时间
    private String time1;//开奖时间
    private String qqgroup;//群
    private int prize;//开奖 0/1
    private String prizeyb;


    public String getPrizeyb() {
        return prizeyb;
    }

    public void setPrizeyb(String prizeyb) {
        this.prizeyb = prizeyb;
    }

    private String hb;
    private int _this = -1;
    private int getR(int [] probability,int r){
        int rr=0;
        for (int i = 0; i <= r; i++) {
            rr+=probability[i];
        }
        return rr;
    }
    public void card(int[] probability) {
        //是否第26次 必得
//        if(getTe()==0&&addNUm()==25){
//            _this= BlessingConst.晋升虎;
//            jingSheng=jingSheng+1;//
//            return;
//        }


//        if(Util.getTime(0)>20220129)
//            probability = (newBrobability);
//        else if(Util.getTime(0)>20220124)
//            probability = (probability0);




        if(Util.getTime(0)>20230119|| BaseListen.isManager(qq)){
            //触发特殊buff 加速集齐
            if(randomCard()){
                return;
            }
        }

        if(Util.isChuXi()){
            probability= new int[]{10, 13, 20, 20, 10, 17, 10};
        }else if (Util.getTime(0) > 20230115 ||  BaseListen.isManager(qq)){
            probability = probability0;
        }

        if(getTe() >=1){

            probability= new int[]{18, 18, 13, 13, 17, 17, 4};
        }



        int r=(int)(Math.random()*100);
        if(r<getR(probability,BlessingConst.健康虎)){
            _this= BlessingConst.健康虎;
            jianKang=jianKang+1;//
        }else if(r<getR(probability,BlessingConst.幸福虎)){
            _this= BlessingConst.幸福虎;
            xingFu=xingFu+1;//
        }else if(r<getR(probability,BlessingConst.晋升虎)){
//            if(getTe()>0){
//                if(Math.random()*100>80){
//                    _this= BlessingConst.幸福虎;
//                    xingFu=xingFu+1;//
//                    return;
//                }
//
//            }
                _this= BlessingConst.晋升虎;
                jingSheng=jingSheng+1;//

        }else if(r<getR(probability,BlessingConst.招财虎)){
            if(getTe()>0){
                if(Math.random()*100>60){
                    _this= BlessingConst.幸福虎;
                    xingFu=xingFu+1;//
                    return;
                }

            }
            _this= BlessingConst.招财虎;
            zhaoCai=zhaoCai+1;//
        }else if(r<getR(probability,BlessingConst.平安虎)){
            _this= BlessingConst.平安虎;
            pingAn=pingAn+1;//
        }else if(r<getR(probability,BlessingConst.好运虎)){
            _this= BlessingConst.好运虎;
            haoYun=haoYun+1;//
        }else if(r<getR(probability,BlessingConst.万能卡)){
//            if(getTe()>0){
//                if(Math.random()*100>80){
//                    _this= BlessingConst.幸福虎;
//                    xingFu=xingFu+1;//
//                    return;
//                }
//
//            }
            _this= BlessingConst.万能卡;
            universal=universal+1;//
        }else{
            _this= BlessingConst.没抽中;
        }
    }

    private  String getCard(){
        return BlessingConst.getConst(_this);
    }
    public String doc(){
        if(getWEi()){
            String t= ""+getIcon(1)+"恭喜你获得一张 [ " +getCard()+" ]" +
                    " \n"+getIcon(1)+"除夕当天回复【团团圆圆】可以领取一张万能卡哦"+
                    " \n"+getIcon(1)+"万能卡：x" +universal+
                    " \n"+getIcon(1)+"健康兔：x" +jianKang+
                    " \n"+getIcon(1)+"幸福兔：x" +xingFu+
                    " \n"+getIcon(1)+"晋升兔：x" +jingSheng+
                    " \n"+getIcon(1)+"招财兔：x" +zhaoCai+
                    " \n"+getIcon(1)+"平安兔：x" +pingAn+
                    " \n"+getIcon(1)+"好运兔：x" +haoYun+
                    "\n-----------------------" +
                    "\n恭喜你集齐"+getTe()+"套福卡，记得大年晚8点来开奖哦，一起瓜分大量悦币和 红包" ;
            return t;

        }
        String t=getIcon(1)+"恭喜你获得一张 [ " +getCard()+" ]" +
                " \n"+getIcon(1)+"除夕当天回复【团团圆圆】可以领取一张万能卡哦"+
                " \n"+getIcon(1)+"万能卡：x" +universal+
                " \n"+getIcon(1)+"健康兔：x" +jianKang+
                " \n"+getIcon(1)+"幸福兔：x" +xingFu+
                " \n"+getIcon(1)+"晋升兔：x" +jingSheng+
                " \n"+getIcon(1)+"招财兔：x" +zhaoCai+
                " \n"+getIcon(1)+"平安兔：x" +pingAn+
                " \n"+getIcon(1)+"好运兔：x" +haoYun+
                "\n-----------------------" +
                "\n当前未集齐一套哦" +
                "\n集齐一套可在除夕夜8点开奖";
        return t;

    }

    public String getMsg(int a){
        if(getWEi()){
            String t= ""+getIcon(1)+"万能卡：x" +universal+
                    " \n"+getIcon(1)+"健康兔：x" +jianKang+
                    " \n"+getIcon(1)+"幸福兔：x" +xingFu+
                    " \n"+getIcon(1)+"晋升兔：x" +jingSheng+
                    " \n"+getIcon(1)+"招财兔：x" +zhaoCai+
                    " \n"+getIcon(1)+"平安兔：x" +pingAn+
                    " \n"+getIcon(1)+"好运兔：x" +haoYun+
                    "\n-----------------------" +
//                    "\n恭喜你集齐一套兔卡，记得大年晚8点来开奖，一起瓜分大量悦币和 红包哦" ;
                    (prize==0?"\n恭喜你集齐"+getTe()+"套福卡\n回复【大吉大利】 领取奖励！":"\n恭喜获得悦币："+prizeyb+"\n恭喜获得红包："+hb) ;
            return t;

        }
        String t= ""+
               getIcon(1)+"除夕当天回复【团团圆圆】可以领取一张万能卡哦"+
                " \n"+getIcon(1)+"万能卡：x" +universal+
                " \n"+getIcon(1)+"健康兔：x" +jianKang+
                " \n"+getIcon(1)+"幸福兔：x" +xingFu+
                " \n"+getIcon(1)+"晋升兔：x" +jingSheng+
                " \n"+getIcon(1)+"招财兔：x" +zhaoCai+
                " \n"+getIcon(1)+"平安兔：x" +pingAn+
                " \n"+getIcon(1)+"好运兔：x" +haoYun+
                "\n-----------------------" +
                "\n当前未集齐一套福卡哦";
        return t;

    }
    /**
     * 是否集齐
     * @return
     */
    public boolean getWEi(){
        if(jianKang!=0&&xingFu!=0&&jingSheng!=0&&zhaoCai!=0&&pingAn!=0&&haoYun!=0){
            return true;
        }

        return false;

    }
    public String getMsg() {
        if(_this==BlessingConst.没抽中){
            return "没抽中卡哦";
        }
        String msg=doc();
        return msg;
    }

    public String getC() {
        return _this+"";
    }

    public String zsCard(int cardId) {
        if(BlessingConst.幸福虎==cardId){
            if(xingFu>0){
                xingFu--;
                return "转赠成功";
            }
            return "转赠失败";
        }else if(BlessingConst.好运虎==cardId){
            if(haoYun>0){
                haoYun--;
                return "转赠成功";
            }
            return "转赠失败";
        }else if(BlessingConst.晋升虎==cardId){
            if(jingSheng>0){
                jingSheng--;
                return "转赠成功";
            }
            return "转赠失败";
        }else if(BlessingConst.招财虎==cardId){
            if(zhaoCai>0){
                zhaoCai--;
                return "转赠成功";
            }
            return "转赠失败";
        }else if(BlessingConst.健康虎==cardId){
            if(jianKang>0){
                jianKang--;
                return "转赠成功";
            }
            return "转赠失败";
        }else if(BlessingConst.平安虎==cardId){
            if(pingAn>0){
                pingAn--;
                return "转赠成功";
            }
            return "转赠失败";
        }else if(BlessingConst.万能卡==cardId){
            if(universal>0){
                universal--;
                return "转赠成功";
            }
            return "转赠失败";
        }else{
            return "转赠失败";
        }

    }
    private String getIcon(int i){
        String s="";
        for (int i1 = 0; i1 < i; i1++) {
            s+=icon;
        }
        return s;
    }

    /**
     * 没集齐的情况下帮助集齐
     * @return
     */
    private boolean randomCard(){
        Integer[] a= notCard();
        if(a==null)return false;
        addCard(a[(int)(Math.random()*a.length)]);
        return true;
    }
    private Integer[] notCard(){
        int cardId = 0;
        ArrayList<Integer> list = new ArrayList<>();
        if(jianKang==0){
            list.add(BlessingConst.健康虎);
        } if(xingFu==cardId){
            list.add(BlessingConst.幸福虎);
        } if(jingSheng==cardId){
            list.add(BlessingConst.晋升虎);
        } if(pingAn==cardId){
//            pingAn++;
            list.add(BlessingConst.平安虎);
        } if(haoYun==cardId){
//            haoYun++;
            list.add(BlessingConst.好运虎);
        } if(zhaoCai==cardId){
            list.add(BlessingConst.招财虎);
        }
        if(list.size()==0)return null;//齐了
        Integer[] as = new Integer[list.size()];
        for (int i = 0; i < list.size(); i++) {
            as[i] = list.get(i);
        }
        return as;
    }
    public void jianUn(){
        setUniversal(universal-1);
    }
    public void addCard(int cardId) {
        if(BlessingConst.健康虎==cardId){
            jianKang++;
        }else if(BlessingConst.幸福虎==cardId){
            xingFu++;
        }else if(BlessingConst.晋升虎==cardId){
            jingSheng++;
        }else if(BlessingConst.平安虎==cardId){
            pingAn++;
        }else if(BlessingConst.好运虎==cardId){
            haoYun++;
        }else if(BlessingConst.招财虎==cardId){
            zhaoCai++;
        }else if(BlessingConst.万能卡==cardId){
            universal++;
        }
        _this =cardId;
    }
    private int addNUm(){
        return jianKang+pingAn+haoYun+zhaoCai+jingSheng+xingFu;
    }
    public ResultBlessing getJiiang(Integer vFnumber) {
        ResultBlessing r=new ResultBlessing();
        r.ditry(vFnumber,getTe());
        return r;
    }

    private int getTe(){

        int[] na = {
            jianKang,
            xingFu,
            jingSheng,
            pingAn,
            haoYun,
            zhaoCai
        };
        int i = na[0];
        for (int i1 = 0; i1 < na.length; i1++) {
            if(na[i1]<i){
                i = na[i1];
            }
        }
        _this = i;

        return i;
    }
    private int geti(){
        int[] na = {
                jianKang,
                xingFu,
                jingSheng,
                pingAn,
                haoYun,
                zhaoCai
        };
        int i = na[0];
        int ii = 0;
        for (int i1 = 0; i1 < na.length; i1++) {
            if(na[i1]<i){
                i = na[i1];
                ii = i1;
            }
        }
        return ii;
    }
    public void setWn() {
        universal=universal+1;
    }

    public BlessingEntry build(String qq, String group) {

                this.setQq(qq);
                this.setQqgroup(group);
                this.setTime(Util.getTime());//开始时间
                this.setTime0(null);
                this.setTime1(null);
//                if(Util.getTime(0)>20220129)
//                    this.card(newBrobability);
//                else if(Util.getTime(0)>20220124)
                    this.card(probability);
//                else
//                    this.card(probability);
                return this;
    }
}
